package com.sanbuTech.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sanbuTech.util.CookieUtils;

public class BaseController {
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);
	@Autowired
	protected HttpServletRequest request;

	/*
	 * @Autowired
	 * 
	 * @Qualifier("memcachedClient") protected MemcachedClient memcachedClient;
	 */

	/***
	 * 获取客户端IP地址 这里通过了Nginx获取;X-Real-IP,X-Forwarded-For
	 * 
	 * @param request
	 * @return
	 */
	public String getClientIP() {
		String ip = request.getHeader("X-Real-IP");
		if (ip == null || ip.length() == 0) {
			ip = request.getHeader("X-Forwarded-For");
		}
		return ip;
	}

	/**
	 * 通用获取自动生成id的接口, 获取id, 用于创建数据
	 * 
	 * @return
	 * @RequestMapping("/getGeneratedId")
	 * @ResponseBody public Object getGeneratedId(@RequestBody JSONObject
	 *               jsonObj) { EventType et = null; int eventTypeIndex =
	 *               jsonObj.getIntValue("eventType"); //所需的id类型 int amount =
	 *               jsonObj.getIntValue("amount"); //id需求数量
	 * 
	 *               if(eventTypeIndex == 0){ return new ResultObject(false,
	 *               SysErrorCode.NULL_PARAM); }
	 * 
	 *               if(amount == 0){ amount = 1; }
	 * 
	 *               et = EventType.findByIndex(eventTypeIndex);
	 * 
	 *               if(et == null){ return new ResultObject(false,
	 *               SysErrorCode.EVENTYPE_MAPPING_ERROR); }
	 * 
	 *               Long orgId = this.getCurrentUser().getOrgId();
	 * 
	 *               String[] idArr = new String[amount]; for(int i = 0; i <
	 *               amount; i++){ idArr[i] = cppServer.getFlowID("org_id=" +
	 *               orgId + "&event_type=" + et.getIndex()); }
	 * 
	 *               ResultObject obj = new ResultObject(); obj.setData(idArr);
	 *               return obj; }
	 */

	/**
	 * 在json参数中获取组织机构id,
	 * 
	 * @param jsonObj
	 * @return
	 * 
	 *         protected long getOrgIdFromParam(JSONObject jsonObj){ String
	 *         orgIdKey = "orgId"; return this.getOrgIdFromParam(jsonObj,
	 *         orgIdKey); } protected long getOrgIdFromParam(JSONObject jsonObj,
	 *         String orgIdKey){ long orgId = 0l; if(jsonObj != null){ orgId =
	 *         jsonObj.getLongValue(orgIdKey); } return (orgId == 0l)?
	 *         this.getCurrentUser().getOrgId() : orgId; }
	 */

	/**
	 * 获取用户数据对象
	 * 
	 * @return protected UserVo getCurrentUser(){ return (UserVo)
	 *         request.getAttribute(TSSystemConstant.REQUEST_USER); }
	 */


	protected void printSimpleResult(HttpServletResponse response, String responseText) {
		try {
			PrintWriter out = this.getPrintWriter(response);
			out.print(responseText);
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取页面输出对象
	 * 
	 * @param response
	 * @return
	 * @throws IOException
	 */
	protected PrintWriter getPrintWriter(HttpServletResponse response)
			throws IOException {
		// response.setCharacterEncoding("UTF-8");
//		response.setContentType("application/json;charset=UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		return response.getWriter();
	}

	/**
	 * 从cookies中获取token值
	 * 
	 * @return
	 */
	protected String getToken() {
		return CookieUtils.getCookieValue(request, "token");
	}

}
